
estimation_data %<>% 
  mutate(g_did = case_when(validated_election_outcome == "Elected" ~ 0,
                           TRUE ~ 3))

#democracy
cs_result_1 <- att_gt(
  yname = "avg_score_democ_support",           # outcome variable
  tname = "A",        # time variable
  idname = "ID",         # unit identifier
  gname = "g_did",           # first period treated (0 if never treated)
  xformla = ~1,          # no covariates; include covariates like ~X1 + X2 if needed
  data = estimation_data,
  panel = TRUE           # TRUE since this is panel data
)

agg_effects_1 <- aggte(cs_result_1, type = "dynamic")


#trust in elections
cs_result_2 <- att_gt(
  yname = "avg_score_trust_elect",           # outcome variable
  tname = "A",        # time variable
  idname = "ID",         # unit identifier
  gname = "g_did",           # first period treated (0 if never treated)
  xformla = ~1,          # no covariates; include covariates like ~X1 + X2 if needed
  data = estimation_data,
  panel = TRUE           # TRUE since this is panel data
)

agg_effects_2 <- aggte(cs_result_2, type = "dynamic")

#outgroup animosity - politics
cs_result_3 <- att_gt(
  yname = "avg_score_dislike_pol",           # outcome variable
  tname = "A",        # time variable
  idname = "ID",         # unit identifier
  gname = "g_did",           # first period treated (0 if never treated)
  xformla = ~1,          # no covariates; include covariates like ~X1 + X2 if needed
  data = estimation_data,
  panel = TRUE           # TRUE since this is panel data
)

agg_effects_3 <- aggte(cs_result_3, type = "dynamic")

#outgroup animosity - ethnicity
cs_result_4 <- att_gt(
  yname = "avg_score_dislike_group",           # outcome variable
  tname = "A",        # time variable
  idname = "ID",         # unit identifier
  gname = "g_did",           # first period treated (0 if never treated)
  xformla = ~1,          # no covariates; include covariates like ~X1 + X2 if needed
  data = estimation_data,
  panel = TRUE           # TRUE since this is panel data
)

agg_effects_4 <- aggte(cs_result_4, type = "dynamic")


model1 <- data.frame(
  term = "ATT (Callaway Sant'Anna)",
  estimate = agg_effects_1$overall.att,
  std.error = agg_effects_1$overall.se,
  out = "Support for Democracy"
)

model2 <- data.frame(
  term = "ATT (Callaway Sant'Anna)",
  estimate = agg_effects_2$overall.att,
  std.error = agg_effects_2$overall.se,
  out = "Trust in Elections"
  
)

model3 <- data.frame(
  term = "ATT (Callaway Sant'Anna)",
  estimate = agg_effects_3$overall.att,
  std.error = agg_effects_3$overall.se,
  out = "Partisan Animosity"
)

model4 <- data.frame(
  term = "ATT (Callaway Sant'Anna)",
  estimate = agg_effects_4$overall.att,
  std.error = agg_effects_4$overall.se,
  out = "Ethnic Animosity"
)

model1b <- lm_robust(avg_score_democ_support ~ validated_election_outcome*pre_post, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "validated_election_outcomeNot Elected:pre_post") %>% 
  mutate(term = "ATT (Canonical)") %>% mutate(out = "Support for Democracy") %>% select(term, estimate, std.error, out)

model2b <- lm_robust(avg_score_trust_elect ~ validated_election_outcome*pre_post, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "validated_election_outcomeNot Elected:pre_post") %>% 
  mutate(term = "ATT (Canonical)") %>% mutate(out = "Trust in Elections") %>% select(term, estimate, std.error, out)

model3b <- lm_robust(avg_score_dislike_pol ~ validated_election_outcome*pre_post, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "validated_election_outcomeNot Elected:pre_post") %>% 
  mutate(term = "ATT (Canonical)") %>% mutate(out = "Partisan Animosity") %>% select(term, estimate, std.error, out)

model4b <- lm_robust(avg_score_dislike_group ~ validated_election_outcome*pre_post, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "validated_election_outcomeNot Elected:pre_post") %>% 
  mutate(term = "ATT (Canonical)") %>% mutate(out = "Ethnic Animosity") %>% select(term, estimate, std.error, out)



plot <-
  bind_rows(model1, model2, model3, model4,
            model1b, model2b, model3b, model4b) %>%
  ggplot(aes(x=estimate, y = out, color = term, group = term)) +
  geom_point(position = position_dodge(width = 0.2)) +
  geom_errorbarh(aes(xmin = (estimate - 1.96*std.error), xmax = (estimate + 1.96*std.error), height = 0), position = position_dodge(width = 0.2)) +
  theme_bw() + 
  ylab("") + 
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(), 
        panel.grid.major.y = element_line(size = .2)) + 
  theme(legend.title = element_blank()) + 
  theme(legend.position = "bottom") +
  theme(strip.background =element_rect(fill="white")) +
  xlab("Effect of Electoral Defeat on Grievance") +
  geom_vline(xintercept = 0, linetype = "dashed") +
  theme(strip.text.y = element_text(angle = 90)) +
  scale_color_manual(values = c("darkgrey", "black"))


ggsave(plot = plot, "./outputs/figures/figure_a8.pdf", width = 8, height = 4)
ggsave(plot = plot, "./outputs/figures/figure_a8.eps", device = "eps", width = 8, height = 4)
